// ======变量定义,要修改======
def projectName = 'template'
// =========================

// 项目路径
def projectDir = './' + projectName

// jenkins上配置的镜像仓库登录账户
// def registryCredentialsId = 'admin_harbor_credentid'
// 镜像仓库
def localRegistryAddress = 'registey.docker.108sq.org'
// 镜像地址
def imageUrl = localRegistryAddress + '/108sq/' + projectName + ':latest'

 //git registry的credentialsId
 def gitCredentialsId = 'jenkins_git'
 //git源码仓库地址
 def gitUrl = 'git.108sq.org:8080/scm/git/java/' + projectName
 def gitbranch = 'develop'

//Dockerfile路径
def dockerfilePath = 'jenkins/test/Dockerfile'


node {

    stage('Prepare') {
        withCredentials([[$class          : 'UsernamePasswordMultiBinding', credentialsId: gitCredentialsId,
                          usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD']]) {
            sh 'rm -rf ' + projectName
        }
        checkout([$class: 'GitSCM', branches: [[name: '*/master-test']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'jinlg-git', url: 'https://gitee.com/SnailJin/TestWeb.git']]])
    }


    stage('mvn build') {
        dir(projectDir) {
            sh 'mvn -version'
            sh 'mvn  -Dmaven.test.skip=true clean package'
            sh 'cp target/*.jar /home/'
        }
    }

    stage('image build') {
        dir(projectDir) {
            sh 'docker ps -a | grep ' + projectName + " | awk '{print \$1}' | xargs docker stop" + ' || true'
            sh 'docker ps -a | grep ' + projectName + " | awk '{print \$1}' | xargs docker rm" + ' || true'
            sh 'docker rmi ' + imageUrl + ' || true'
            sh 'docker build -t ' + imageUrl + ' -f ' + dockerfilePath + ' .'
            sh 'docker push ' + imageUrl
        }
    }

    stage('mvn sonar') {
        dir(projectDir) {
            sh 'mvn sonar:sonar'
        }
    }


}
